package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.system.domain.Drug;
import com.ruoyi.system.domain.DrugVerify;
import com.ruoyi.system.service.IDrugService;
import com.ruoyi.system.service.IDrugVerifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CaseHistoryMapper;
import com.ruoyi.system.domain.CaseHistory;
import com.ruoyi.system.service.ICaseHistoryService;

/**
 * 厦门病历记录Service业务层处理
 * 
 * @author wmb
 * @date 2021-03-12
 */
@Service
public class CaseHistoryServiceImpl implements ICaseHistoryService 
{
    @Autowired
    private CaseHistoryMapper caseHistoryMapper;

    @Autowired
    private IDrugService drugService;

    @Autowired
    private IDrugVerifyService drugVerifyService;
    /**
     * 查询厦门病历记录
     * 
     * @param id 厦门病历记录ID
     * @return 厦门病历记录
     */
    @Override
    public CaseHistory selectCaseHistoryById(Long id)
    {
        return caseHistoryMapper.selectCaseHistoryById(id);
    }

    /**
     * 查询厦门病历记录列表
     * 
     * @param caseHistory 厦门病历记录
     * @return 厦门病历记录
     */
    @Override
    public List<CaseHistory> selectCaseHistoryList(CaseHistory caseHistory)
    {
        return caseHistoryMapper.selectCaseHistoryList(caseHistory);
    }

    /**
     * 新增厦门病历记录
     * 
     * @param caseHistory 厦门病历记录
     * @return 结果
     */
    @Override
    public int insertCaseHistory(CaseHistory caseHistory)
    {
        caseHistory.setCreateTime(DateUtils.getNowDate());
        return caseHistoryMapper.insertCaseHistory(caseHistory);
    }

    /**
     * 修改厦门病历记录
     * 
     * @param caseHistory 厦门病历记录
     * @return 结果
     */
    @Override
    public int updateCaseHistory(CaseHistory caseHistory)
    {
        return caseHistoryMapper.updateCaseHistory(caseHistory);
    }

    /**
     * 批量删除厦门病历记录
     * 
     * @param ids 需要删除的厦门病历记录ID
     * @return 结果
     */
    @Override
    public int deleteCaseHistoryByIds(Long[] ids)
    {
        return caseHistoryMapper.deleteCaseHistoryByIds(ids);
    }

    /**
     * 删除厦门病历记录信息
     * 
     * @param id 厦门病历记录ID
     * @return 结果
     */
    @Override
    public int deleteCaseHistoryById(Long id)
    {
        return caseHistoryMapper.deleteCaseHistoryById(id);
    }

    @Override
    public void insertBatchCase(List<CaseHistory> caseHistories) {

        Date date = new Date();
        for(CaseHistory caseHistory:caseHistories){

            Drug drug = drugService.selectDrugByName(caseHistory.getDrugName());
            if(drug != null){
                caseHistory.setCreateTime(date);
                this.insertCaseHistory(caseHistory);
                DrugVerify verify = new DrugVerify();
                verify.setAdmissionId(caseHistory.getId());
                //verify.setCode(medical.getCode());
                verify.setDrugId(drug.getId());
                verify.setDrugName(drug.getName());
                drugVerifyService.insertDrugVerify(verify);
            }
        }
    }

    @Override
    public Long insertCase(CaseHistory caseHistory) {

        Drug drug = drugService.selectDrugByName(caseHistory.getDrugName());
        if(drug != null){
            caseHistory.setCreateTime(DateUtils.getNowDate());
            this.insertCaseHistory(caseHistory);
            DrugVerify verify = new DrugVerify();
            verify.setAdmissionId(caseHistory.getId());
            verify.setDrugId(drug.getId());
            verify.setDrugName(drug.getName());
            drugVerifyService.insertDrugVerify(verify);
            return caseHistory.getId();
        }
        return null;
    }
}
